package com.jn.erp.web.api.course.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.common.enums.ErrorEnum;
import com.jn.erp.web.api.course.entity.TCourse;
import com.jn.erp.web.api.course.entity.TCoursePrice;
import com.jn.erp.web.api.course.entity.TCourseSchedule;
import com.jn.erp.web.api.course.entity.dto.TCourseDto;
import com.jn.erp.web.api.course.entity.vo.TCourseVo;
import com.jn.erp.web.api.course.mapper.TCourseMapper;
import com.jn.erp.web.api.course.mapper.TCoursePriceMapper;
import com.jn.erp.web.api.course.mapper.TCourseScheduleMapper;
import com.jn.erp.web.api.course.service.TCourseScheduleService;
import com.jn.erp.web.api.course.service.TCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-08-29
 */
@Service
public class TCourseServiceImpl extends ServiceImpl<TCourseMapper, TCourse> implements TCourseService {
    @Autowired
    private TCoursePriceMapper tCoursePriceMapper;
    @Autowired
    private TCourseScheduleMapper tCourseScheduleMapper;
    @Override
    public String saveCourse(TCourseVo tCourseVo) {
        //课程名不可以重复
        List<TCourse> tCourses = baseMapper.selectList(new QueryWrapper<TCourse>().eq("name", tCourseVo.getName()).eq("is_delete", "0"));
        if(!CollectionUtils.isEmpty(tCourses)){
            return ErrorEnum.E101.getValue();
        }
        tCourseVo.setCreateDate(LocalDateTime.now());
        tCourseVo.setUpdateDate(LocalDateTime.now());
        tCourseVo.setIsDelete("0");
        baseMapper.insert(tCourseVo);
        List<TCoursePrice> tCoursePriceList = tCourseVo.gettCoursePriceList();
        if(!CollectionUtils.isEmpty(tCoursePriceList)){
            tCoursePriceList.stream().forEach(u->{
                u.setCourseId(tCourseVo.getId());
                u.setCreateDate(LocalDateTime.now());
                u.setCreateUser(tCourseVo.getCreateUser());
                u.setUpdateDate(LocalDateTime.now());
                u.setIsDelete("0");
            });
            tCoursePriceMapper.insertBatch(tCoursePriceList);
        }
        List<TCourseSchedule> tCourseScheduleList = tCourseVo.gettCourseScheduleList();
        if(!CollectionUtils.isEmpty(tCourseScheduleList)){
            tCourseScheduleList.stream().forEach(u->{
                u.setCourseId(tCourseVo.getId());
                u.setCreateDate(LocalDateTime.now());
                u.setCreateUser(tCourseVo.getCreateUser());
                u.setUpdateDate(LocalDateTime.now());
                u.setIsDelete("0");
            });
            tCourseScheduleMapper.insertBatch(tCourseScheduleList);
        }
        return "S200";
    }

    @Override
    public List<TCourseDto> courseList(TCourseVo tCourseVo) {
        List<TCourseDto> records = this.baseMapper.courseList(tCourseVo);
        if(!CollectionUtils.isEmpty(records)){
            for(TCourseDto tCourseDto : records){
                QueryWrapper<TCoursePrice> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("course_id",tCourseDto.getId());
                List<TCoursePrice> tCoursePrices = tCoursePriceMapper.selectList(queryWrapper);
                tCourseDto.settCoursePriceList(tCoursePrices);
                Set<Integer> payTypeSets=tCoursePrices.stream().map(TCoursePrice::getPayType).collect(Collectors.toSet());
                String payType  = payTypeSets.stream().map(u->u.toString()).collect(Collectors.joining(","));
                tCourseDto.setPayType(payType);
                //在读学员后续补充
            }
        }


        return records;
    }

    @Override
    public String updateCourse(TCourseVo tCourseVo) {
        //课程名不可以重复
        List<TCourse> tCourses = baseMapper.selectList(new QueryWrapper<TCourse>().eq("name", tCourseVo.getName()).eq("is_delete", "0").ne("id",tCourseVo.getId()));
        if(!CollectionUtils.isEmpty(tCourses)){
            return ErrorEnum.E101.getValue();
        }
        tCourseVo.setUpdateUser(tCourseVo.getCreateUser());
        tCourseVo.setUpdateDate(LocalDateTime.now());
        baseMapper.updateById(tCourseVo);
        List<TCoursePrice> tCoursePriceList = tCourseVo.gettCoursePriceList();
        if(!CollectionUtils.isEmpty(tCoursePriceList)){

            tCoursePriceMapper.delete(new QueryWrapper<TCoursePrice>().eq("course_id",tCourseVo.getId()));
            tCoursePriceList.stream().forEach(u->{
                u.setCourseId(tCourseVo.getId());
                u.setCreateDate(LocalDateTime.now());
                u.setCreateUser(tCourseVo.getCreateUser());
                u.setUpdateDate(LocalDateTime.now());
                u.setIsDelete("0");
            });
            tCoursePriceMapper.insertBatch(tCoursePriceList);
        }
        List<TCourseSchedule> tCourseScheduleList = tCourseVo.gettCourseScheduleList();
        if(!CollectionUtils.isEmpty(tCourseScheduleList)){
            tCourseScheduleMapper.delete(new QueryWrapper<TCourseSchedule>().eq("course_id",tCourseVo.getId()));
            tCourseScheduleList.stream().forEach(u->{
                u.setCourseId(tCourseVo.getId());
                u.setCreateDate(LocalDateTime.now());
                u.setCreateUser(tCourseVo.getCreateUser());
                u.setUpdateDate(LocalDateTime.now());
                u.setIsDelete("0");
            });
            tCourseScheduleMapper.insertBatch(tCourseScheduleList);
        }
        return "S200";
    }

    @Override
    public Boolean deleteCourse(TCourseVo tCourseVo) {

        tCourseVo.setUpdateUser(tCourseVo.getCreateUser());
        tCourseVo.setUpdateDate(LocalDateTime.now());
        baseMapper.update(tCourseVo,new QueryWrapper<TCourse>().in("id",tCourseVo.getIds()));
        return true;
    }
}
